<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录框背景颜色变化</title>
    <style>
        /* 登录框样式 */

        .login-box {
            width: 300px;
            height: 200px;
            margin: 100px auto;
            padding: 20px;
            border: 1px solid #ccc;
            border-radius: 5px;
        }

        .login-box h2 {
            margin-top: 0;
        }

        .login-box input {
            width: 100%;
            padding: 10px;
            margin-bottom: 15px;
            border: 1px solid #ccc;
            border-radius: 3px;
        }

        .login-box button {
            width: 100%;
            padding: 10px;
            background-color: #007BFF;
            color: white;
            border: none;
            border-radius: 3px;
            cursor: pointer;
        }
    </style>
</head>

<body>
    <div class="login-box">
        <h2>登录</h2>
        <input type="text" placeholder="用户名">
        <input type="password" placeholder="密码">
        <button>登录</button>
    </div>
    <script>
        // 获取登录框元素
        const loginBox = document.querySelector('.login-box');

        // 定义一个函数来生成随机颜色
        function getRandomColor() {
            const letters = '0123456789ABCDEF';
            let color = '#';
            for (let i = 0; i < 6; i++) {
                color += letters[Math.floor(Math.random() * 16)];
            }
            return color;
        }

        // 定时改变背景颜色
        setInterval(() => {
            const randomColor = getRandomColor();
            loginBox.style.backgroundColor = randomColor;
        }, 2000); // 每 2 秒改变一次背景颜色
    </script>
</body>

</html>
